/**
 * Created by chenkun on 2017/6/8.
 */
import Vue from "vue";
import axios from "axios";
import VueAxios from "vue-axios";
import Qs from "qs";
import { gbs } from "@/config";
// import router from 'router'
// import {store} from 'utils'
//axios.defaults.withCredentials=true;
Vue.use(VueAxios, axios);
//axios.defaults.withCredentials=true;
// 动态设置本地和线上接口域名
// console.log(gbs);

Vue.axios.defaults.baseURL = gbs.host;

export default function ({
  type,
  path,
  data,
  fn,
  succFn,
  errFn,
  tokenFlag,
  noLoading,
  headers,
  opts
} = {}) {
  var options = {
    method: type,
    url: path,
    headers: {
      //'Content-Type': 'application/json'
      "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
    }
    // headers: {
    //     'Content-Type': 'application/x-www-form-urlencoded',
    //     'Accept': 'application/json'
    //
    // }
    //withCredentials : true,
    //headers: headers && typeof headers === 'object' ? headers : {}
    // headers: {
    //     'Access-Control-Allow-Origin': 'http://localhost:5000',
    //     'Access-Control-Allow-Credentials': 'true'
    // }
  };
  // this.$store.dispatch('hide_err');
  var me = this;

  var api_flag = true;
  //console.log(store);
  if (api_flag === true) {
    // if (!tokenFlag) {
    //     data.token = (process.env.NODE_ENV == 'development' ? 'a00b0c45-18b7-d697-47e2-7e2d120362fb' : store.get('token')) //||'a00b0c45-18b7-d697-47e2-7e2d120362fb';  //默认都需要设置token
    // }
    options[type === "get" ? "params" : "data"] = data; // Qs.stringify(data);
    options[type === "post" ? "data" : "data"] = Qs.stringify(data);
    // 分发显示加载样式任务
    if (!noLoading) {
      this.$vux.loading.show({
        text: "加载中..."
      });
      // Vue.$indicator.open()
    }
    //axios内置属性均可写在这里
    if (opts && typeof opts === "object") {
      for (var f in opts) {
        options[f] = opts[f];
      }
    }

    // if (this.$store.state.user.userinfo.base_student_id) {
    //   var info = {
    //     base_student_id: this.$store.state.user.userinfo.base_student_id,
    //     date: new Date(),
    //     router: this.$route.path,
    //     api: options.url,
    //     agent: window.userAgent,
    //     way: '1'
    //   };
    //   window.tuoguan_tongji.push(info)
    // }

    Vue.axios(options)
      .then(res => {
        this.$vux.loading.hide();
        fn && fn(res.data)
        if (res.data.error_code == 0) {
          succFn && succFn(res.data.data);
        } else if (res.data.code == "1001" || res.data.code == "1000") {
        } else {
          if (typeof errFn === 'function') {
            errFn && errFn(res.data)
          } else {
            this.$vux.toast.show({
              width: '14em',
              type: "text",
              text: res.data.error_msg
            })
          }

        }
      })
      .catch(err => {
        this.$vux.loading.hide();
        errFn && errFn(err.data);
      });
  } else {
    this.$vux.loading.hide();
  }
}
